import React, { useState, useEffect, useRef } from "react";
import ManualShipmentModal from "./ManualShipmentModal";
import CertificatePrintModal from "./CertificatePrintModal";
import ExpressPrintModal from "./ExpressPrintModal/new.jsx";
import ActionExportExcelModal from "./ActionExportExcelModal";
import OrderRemarksModal from "./OrderRemarksModal";
import Api from '@/pages/OrderModule/orderList/components/Actions/service';
import { kuaiXiaoPrintFunc } from '@/utils/print.jsx';
import { orderBy, groupBy } from "lodash";

//打印数据处理
const formatPrintOrder = (val) => {
	const printArr = [];
	const arr = orderBy(val, 'printSequence');
	const list = groupBy(arr, 'warehouseId');
	for(let i in list) {
		const list1 = groupBy(list[i], 'printer');
		for(let j in list1) {
			const list2 = list1[j];
			list2.map((v, index) => {
				printArr.push({
					...v,
					num: index + 1,
					len: list2.length,
				})
			})
		}
	}
	return printArr;
}

const MulDialog = props => {
	const { visibleDialogName, customDialogProps, searchForm, ...resetProps } = props;

	// 当前打印的面单数据列表
	const [orderList, setOrderList] = useState([]);
	const isPrint = useRef(false);
	const storageData = useRef([]);
	const pointer = useRef(0);

	useEffect(()=>{
   if(orderList?.length){
		pointer.current = 0;
		executePrintFunc();
	 }
	}, [orderList])

	const executePrintFunc = () => {
		const promise = new Promise(resolve=>{
			if(pointer.current == orderList?.length){
			 return resolve()
			}
			kuaiXiaoPrintFunc(orderList[pointer.current], (res)=>{
				const { status, logPage, printTaskId, orderSn } = res;
				if(status){
					Api.printSuccessOp({ 
						logPage, 
						printTaskId, 
						systemNos: [orderSn] 
					});
				}
				pointer.current++;
				executePrintFunc();
			})
		})
	
		promise.then(()=>{
			const awaitArr = storageData.current;
			if(awaitArr.length){
				setOrderList(awaitArr[0]);
				awaitArr.splice(0, 1);
			}else{
				isPrint.current = false;
				setOrderList([]);
			}
		})
	}

	const realProps = {
		searchForm,
		...resetProps,
		...customDialogProps,
		handleSavaPrint(val){
		const arr = formatPrintOrder(val);
	   if(isPrint.current){
				console.log('暂存')
				storageData.current.push(arr);
			}else{
				isPrint.current = true;
				setOrderList(arr);
			}
		}
	};
	return (
		<>
			{/** 手动发货 */}
			{visibleDialogName === "ManualShipmentModal" && <ManualShipmentModal {...realProps} commandText="手动发货确定后" />}
			{/** 合格证批量打印 */}
			{visibleDialogName === "CertificatePrintModal" && (
				<CertificatePrintModal {...realProps} commandText="合格证批量打印确定后" />
			)}
			{/** 面单批量打印 */}
			{/* {visibleDialogName === "ExpressPrintModal" && <ExpressPrintModal {...realProps} commandText="快递打印确定后" />} */}
			{visibleDialogName === "ExpressPrintModal" && <ExpressPrintModal {...realProps} commandText="快递打印确定后" />}
			{/* 导出 */}
			{visibleDialogName === "ActionExportExcelModal" && <ActionExportExcelModal {...realProps} />}
			{/** 订单备注 */}
			{visibleDialogName === "OrderRemarksModal" && <OrderRemarksModal {...realProps} />}
		</>
	);
};
export default React.memo(MulDialog);
